<?php
/*
 * FileName appController.php
 * Date		2012-7-25
 * Author	jime
 * Mail 	me@joyphper.net
 * QQ		97142822
 * Copyright (C) 2012 joyphper.net
 */
if (! defined ( 'App_PATH' ))exit ( 'request error' );
import("core/AppViews.php");
class AppBaseController{

	private $pramas; //运行参数，包括$_GET/$_POST/$_COOKIE等
	private $app;
	private $view; 	 //视图控制器对象

	/**
	 * 控制器初始化参数
	 */
	public function AppBaseController($app)
	{
		$this->app = $app;

		//设置初始化参数
		foreach ( $_GET as $_k => $_v )
			$_GET[$_k]	= $this->_RunMagicQuotes ( $_v );

		foreach ( $_POST as $_k => $_v )
			$_POST[$_k]	=$this->_RunMagicQuotes ( $_v );

		foreach ( $_COOKIE as $_k => $_v )
			$_COOKIE[$_k]	= $this->_RunMagicQuotes ( $_v );

		$this->pramas=array_merge($_GET,$_POST,$_COOKIE);//生成整站处理参数

		if(!C('Debug'))unset($_GET,$_POST);//不开启Debug销除$_GET,$_POST变量
	}
	/**
	 * 在注销$_GET/$_POST之后能过此方法来获取运行参数
	 */
	protected function getVar($svar="")
	{
		if(empty($svar))
		{
			return $this->pramas;
		}
		else
		{
			if(isset($this->pramas[$svar]))
				return $this->pramas[$svar];
			else
				return false;
		}
	}

	/**
	 * 在注销$_GET/$_POST之后能过此方法来设置运行参数
	 */
	protected function setVar($name,$value)
	{
		$this->pramas[$name]=$value;//设置全局访问变量
	}
	/**
	*获取系统保存的session
	*/
	protected function getSession($svar=""){
		if(empty($svar))
		{
			return $_SESSION;
		}
		else
		{
			if(isset($_SESSION[$svar]))
				return $_SESSION[$svar];
			else
				return false;
		}
	}
	/**
	 * 设置一个Session
	 */
	protected function setSession($key,$val){
		if(!empty($key) && !empty($val))
			$_SESSION[$key] = $val;
	}
	/**
	*销毁系统session
	*/
	protected function removeSession($svar=""){
		if(empty($svar))
			unset($_SESSION);
		else
			if(isset($_SESSION[$svar]))
				unset($_SESSION[$svar]);
	}
	/**
	 * 字符转义
	 */
	private function _RunMagicQuotes(&$svar)
	{
		if (! get_magic_quotes_gpc ())
		{
			if (is_array ( $svar ))
				foreach ( $svar as $_k => $_v )$svar [$_k] = $this->_RunMagicQuotes ( $_v );
			else
				$svar = addslashes ( $svar );
		}
		return $svar;
	}
	/**
	 *打出系统文件日志，便于调试，应用log4php插件
	 */
	protected function logger($loggerName=""){
		if(empty($loggerName))
			$loggerName="System";
		import('misc/log4php/Logger.php');

		Logger::configure(App_PATH.'/misc/log4php.properties');

		$logger = Logger::getLogger($loggerName);

		return $logger;
	}
	protected function getService($S)
    {
    	// 检查类名称是否正确，以保证类定义文件载入的安全性
		if(preg_match('/[^a-z0-9\-_.]/i', $S))appError($S."类名称错误，请检查。");

		//载入基础service类
    	import("core/AppBaseService.php");

    	//创建业务类路径
		$SFile=C('sys_dir.service').'/'.$this->app.'/'.$S.'.class.php';

		//如果模型文件不存在则跳出404错误
		if(is_file(App_PATH.'/'.$SFile))
			import($SFile); //引入模型文件
		else
			appError(App_PATH."/".$SFile."类不存在！");

		$Service="";

		eval('$'."Service=new ".$S."();"); //遵守命名规范

		return $Service;
    }
    /**
     * 给视图赋值
     */
    protected function assign($key,$val){
    	if(empty($this->view)) $this->view = new AppViews();
    	$this->view->assign($key,$val);
	}
	/**
	 * 调用模板，实现模板展现
	 */
    protected function display($tmpl){
    	if(empty($this->view)) $this->view = new AppViews();
		$this->view->display($tmpl);
    }
}
?>
